<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务编号" prop="num">
        <el-input
          v-model="queryParams.num"
          placeholder="请输入任务编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择任务状态" style="width: 160px" clearable>
          <el-option
            v-for="dict in asset_task_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="stockList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="任务编号" align="center" prop="num" />
      <el-table-column label="资产总数" align="center" prop="assetCount" />
      <el-table-column label="资产总值" align="center" prop="assetWorth" />
      <el-table-column label="出售总数" align="center" prop="saleCount" />
      <el-table-column label="出售总值" align="center" prop="saleWorth" />
      <el-table-column label="报废总数" align="center" prop="scrapCount" />
      <el-table-column label="报废总值" align="center" prop="scrapWorth" />
      <el-table-column label="丢失数量" align="center" prop="loseCount" />
      <el-table-column label="丢失价值" align="center" prop="loseWorth" />
      <el-table-column label="维修次数" align="center" prop="maintainCount" />
      <el-table-column label="维修花费" align="center" prop="maintainWorth" />
      <el-table-column label="采购次数" align="center" prop="purchaseCount" />
      <el-table-column label="采购总值" align="center" prop="purchaseWorth" />
      <el-table-column label="盘点周期" align="center" prop="startTime" width="180">
        <template #default="scope">
          {{scope.row.startTime}} ~ {{scope.row.endTime}}
        </template>
      </el-table-column>
      <el-table-column label="任务状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="asset_task_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Finished" @click="handleStock(scope.row)"  v-hasPermi="['asset:stock:list']">明细</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-if="scope.row.status != '9'" v-hasPermi="['asset:stock:edit']">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改盘点任务对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="stockRef" :model="form" :rules="rules" label-width="80px">
      <el-tabs v-model="tab">
      <el-tab-pane label="任务信息" name="base">
        <el-form-item label="任务编号" prop="num">
          <el-input v-model="form.num" placeholder="请输入任务编号" readonly/>
        </el-form-item>
        <el-form-item label="盘点要求" prop="remark">
          <el-input v-model="form.remark" type="textarea" :rows="3" placeholder="请输入盘点要求" readonly/>
        </el-form-item>
        <el-form-item label="开始周期" prop="startTime">
          <el-input v-model="form.startTime" placeholder="请输入盘点计算的开始时间" readonly/>
        </el-form-item>
        <el-form-item label="结束周期" prop="endTime">
          <el-input v-model="form.endTime" placeholder="请输入盘点计算的结束时间" readonly/>
        </el-form-item>
        <el-form-item label="任务状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择任务状态" style="width: 160px" clearable>
            <el-option
              v-for="dict in asset_task_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="盘点结果" prop="result">
          <el-input v-model="form.result" type="textarea" :rows="3" placeholder="请输入结果说明" />
        </el-form-item>
      </el-tab-pane>
      <el-tab-pane label="详细信息" name="detail" v-if="form.id">
        <el-row>
          <el-col :span="12">
        <el-form-item label="资产总数" prop="assetCount">
          <el-input v-model="form.assetCount" placeholder="请输入固定资产总数" readonly/>
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="资产总值" prop="assetWorth">
          <el-input v-model="form.assetWorth" placeholder="请输入固定资产总值" readonly/>
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
        <el-form-item label="出售总数" prop="saleCount">
          <el-input v-model="form.saleCount" placeholder="请输入出售总数" readonly/>
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="资产总值" prop="saleWorth">
          <el-input v-model="form.saleWorth" placeholder="请输入出售资产总值" readonly/>
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
        <el-form-item label="报废总数" prop="scrapCount">
          <el-input v-model="form.scrapCount" placeholder="请输入报废资产总数" readonly/>
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="报废总值" prop="scrapWorth">
          <el-input v-model="form.scrapWorth" placeholder="请输入报废资产总值" readonly/>
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
        <el-form-item label="丢失数量" prop="loseCount">
          <el-input v-model="form.loseCount" placeholder="请输入丢失数量" readonly/>
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="丢失总值" prop="loseWorth">
          <el-input v-model="form.loseWorth" placeholder="请输入丢失总值" readonly/>
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
        <el-form-item label="维修次数" prop="maintainCount">
          <el-input v-model="form.maintainCount" placeholder="请输入资产维修次数" readonly/>
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="维修花费" prop="maintainWorth">
          <el-input v-model="form.maintainWorth" placeholder="请输入资产维修花费" readonly/>
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
        <el-form-item label="采购次数" prop="purchaseCount">
          <el-input v-model="form.purchaseCount" placeholder="请输入采购次数" readonly/>
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="采购总值" prop="purchaseWorth">
          <el-input v-model="form.purchaseWorth" placeholder="请输入采购总值" readonly/>
        </el-form-item>
          </el-col>
        </el-row>
      </el-tab-pane>
      </el-tabs>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Stock">
import { listMyStock, getStock, delStock, addStock, updateStock, submitStock } from "@/api/asset/stock";
import { queryDs } from "@/api/dev/dataset";
import router from "@/router";

const { proxy } = getCurrentInstance();
const { asset_task_status } = proxy.useDict('asset_task_status');

const stockList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const tab = ref('base');
const title = ref("");
const userOptions = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderByColumn: 't.id',
    isAsc: 'DESC',
    num: null,
    worth: null,
    assetCount: null,
    assetWorth: null,
    saleCount: null,
    saleWorth: null,
    scrapCount: null,
    scrapWorth: null,
    maintainCount: null,
    maintainWorth: null,
    purchaseCount: null,
    purchaseWorth: null,
    result: null,
    userId: null,
    status: null,
  },
  rules: {
    num: [
      { required: true, message: "任务编号不能为空", trigger: "blur" }
    ],
    userId: [
      { required: true, message: "负责人不能为空", trigger: "blur" }
    ],
    startTime: [
      { required: true, message: "开始时间不能为空", trigger: "blur" }
    ],
    endTime: [
      { required: true, message: "结束时间不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询盘点任务列表 */
function getList() {
  loading.value = true;
  listMyStock(queryParams.value).then(response => {
    stockList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    num: null,
    assetCount: null,
    assetWorth: null,
    saleCount: null,
    saleWorth: null,
    scrapCount: null,
    scrapWorth: null,
    loseCount: null,
    loseWorth: null,
    maintainCount: null,
    maintainWorth: null,
    purchaseCount: null,
    purchaseWorth: null,
    remark: null,
    result: null,
    userId: null,
    status: '0',
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    startTime: null,
    endTime: null
  };
  proxy.resetForm("stockRef");
  tab.value = 'base';
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加盘点任务";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getStock(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改盘点任务";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["stockRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateStock(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addStock(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除选中的盘点任务？').then(function() {
    return delStock(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 提交按钮操作 */
function handleSubmit() {
  proxy.$modal.confirm('是否确认提交盘点任务，编号为"' + row.num + '",提交后将不修改任何数据，并且将盘点结果同步更新到资产信息？').then(function() {
    return submitStock(row.id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("提交成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('asset/stock/export', {
    ...queryParams.value
  }, `stock_${new Date().getTime()}.xlsx`)
}


/** 盘点明细按钮操作 */
function handleStock(row) {
  const stockId = row.id;
  router.push({ path: "/asset/stockDetail/" + stockId});
}

getList();


queryDs({"code":"sys_user_select"}).then(response=>{
    userOptions.value = response.data;
});
</script>
